package com.hoshiibuy.spu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.spu.entity.ExtSpuTags;
import com.hoshiibuy.spu.feign.IdGeneratorService;
import com.hoshiibuy.spu.mapper.ExtSpuTagsMapper;
import com.hoshiibuy.spu.service.IExtSpuTagsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * SPU服务标签Service接口实现类
 *
 * @author : yangcheng
 * @date : 2019/5/5 13:55
 * @mail : maiboyc@163.com
 **/
@Service
public class ExtSpuTagsServiceImpl extends ServiceImpl<ExtSpuTagsMapper, ExtSpuTags> implements IExtSpuTagsService {

    // id生成service
    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * 根据id获取
     * @param id ID
     * @return BaseSpu
     */
    @Cacheable(cacheNames = "ExtSpuTags", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public ExtSpuTags get(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 保存
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ExtSpuTags", allEntries = true)
    @Override
    public boolean save(ExtSpuTags entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null) {
            throw new ServiceException(CommonError.error("新增spu服务标签资料失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        entity.setUpdatedtime(LocalDateTime.now());
        //需要做日期转换
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增spu服务标签资料失败"));
        }
        return true;
    }

    /**
     * 修改
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ExtSpuTags", allEntries = true)
    @Override
    public boolean update(ExtSpuTags entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改spu服务标签资料失败"));
        }
        return true;
    }

    /**
     * 查询
     * @param entity 查询参数实体
     * @return IPage<ExtSpuTags>
     */
    @CacheEvict(cacheNames = "ExtSpuTags", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#entity.pageIndex")
    @Override
    public IPage<ExtSpuTags> List(ExtSpuTags entity) {
        QueryWrapper<ExtSpuTags> queryWrapper = new QueryWrapper();
        IPage<ExtSpuTags> page = new Page();
        if (entity.getPageIndex() != null) {
            page.setCurrent(entity.getPageIndex());
        }
        if (entity.getPageSize() != null) {
            page.setSize(entity.getPageSize());
        }
        if (entity.getSpuid() != null) {
            queryWrapper.eq("spuid", entity.getSpuid());
        }
        if (StringUtils.isNotEmpty(entity.getTagname())) {
            queryWrapper.like("tagname", entity.getTagname());
        }
        if (StringUtils.isNotEmpty(entity.getTagValue())) {
            queryWrapper.eq("tag_value", entity.getTagValue());
        }
        return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 删除
     * @param id ID
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ExtSpuTags", allEntries = true)
    @Override
    public boolean delete(Long id) {
        int row = baseMapper.deleteById(id);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除spu服务标签资料失败"));
        } else {
            return true;
        }
    }
}